package com.example.utils;

import com.example.filter.JWTAuthenticationFilter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class UserUtils {

    /***
     * 获取当前用户Id
     * @return
     */
    public static String getUserId(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes != null && servletRequestAttributes.getRequest() != null) {
            String token = servletRequestAttributes.getRequest().getHeader(JWTAuthenticationFilter.TOKEN);
            return JWTUtils.parseToken(token).getId();
        }
        return null;
    }
}
